home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0921.ZIP / JOYSTK.ARC / JOYDEMO2.PAS < prev    next >
Pascal/Delphi Source File  |  1987-03-29  |  4KB  |  120 lines

  1. program JoyDemo2;
  2.  
  3. {
  4. Author:        David Howorth
  5. Last updated:  March 29, 1987
  6. Application:   A demonstration of procedure ReadJoy, a procedure for
  7.                reading the joystick, using variable parameters.  Shows
  8.                how to use the joystick as a DIRECTION indicator.
  9. }
  10. {$C-}
  11. { Above compiler directive necessary so that 'keypressed' function works
  12.   properly.  Also speeds up screen output.                               }
  13.  
  14. var
  15.   ch      :   char;
  16.   Done    :   boolean;
  17.   InitX,
  18.   InitY   :   integer;
  19.   JoyX,
  20.   JoyY,
  21.   Dummy   :   integer;
  22.   ScreenX,
  23.   ScreenY :   byte;
  24.  
  25. {$I READJOY.INC}
  26.  
  27. {--------------------------------------}
  28.  
  29. procedure SetUpScreen;
  30.  
  31. {-------------}
  32.  
  33. procedure Box(x1,y1,x2,y2 : integer);
  34.  
  35. var
  36.   i : integer;
  37.  
  38. begin
  39.   gotoXY(x1,y1);
  40.   write(char(218));
  41.   for i := x1 + 1 to x2 - 1 do write(char(196));
  42.   write(char(191));
  43.   for i := y1 + 1 to y2 - 1 do
  44.   begin
  45.     gotoXY(x1,i);
  46.     write(char(179));
  47.     gotoXY(x2,i);
  48.     write(char(179));
  49.   end;
  50.   gotoXY(x1,y2);
  51.   write(char(192));
  52.   for i := x1 + 1 to x2 - 1 do write(char(196));
  53.   write(char(217));
  54. end;
  55.  
  56. {-------------}
  57.  
  58. begin
  59.   clrscr;
  60.   writeln('                         JOYSTICK DEMONSTRATION NUMBER 2');
  61.   writeln;
  62.   writeln('                        (Joystick as direction indicator)');
  63.   gotoxy(1,25);
  64.   write('                               Press  ''Q'' to quit.');
  65.   Box(1,5,80,24);
  66. end;
  67.  
  68. {--------------------------------------}
  69.  
  70. begin { main program }
  71.   ReadJoy(InitX,InitY,Dummy,Dummy);          { calibrate joystick }
  72.  
  73. { Note that ReadJoy expects four parameters, even if your program doesn't
  74.   need them all, as it won't if you are only reading one joystick.  As for
  75.   the parameters you don't care about, there's no harm in passing the same
  76.   one twice, as done here.                                                 }
  77.  
  78.   SetUpScreen;
  79.   while keypressed do read(kbd,ch);         { empty keyboard buffer }
  80.   Done := false;
  81.   ScreenX := 40;
  82.   ScreenY := 14;
  83.   repeat
  84.     gotoxy(ScreenX,ScreenY);
  85.     write(' ');
  86.     ReadJoy(JoyX,JoyY,Dummy,Dummy);
  87.     if JoyX > InitX + (InitX div 5)
  88.       then ScreenX := ScreenX + 1
  89.       else if JoyX < InitX - (InitX div 5)
  90.              then ScreenX := ScreenX - 1;
  91.     if JoyY > InitY + (InitY div 5)
  92.       then ScreenY := ScreenY + 1
  93.       else if JoyY < InitY - (InitY div 5)
  94.              then ScreenY := ScreenY - 1;
  95.  
  96. { If all you are interested in is what direction the joystick has been moved,
  97.   all you need to do is compare the current position with the initial position
  98.   calibrated by the program.  If you want your program to be relatively
  99.   portable, you ought to make the comparison in as relative terms as possible,
  100.   i.e., use something like 'if JoyX > InitX + (InitX div 5)', rather than
  101.   something like 'if JoyX > InitX + 10'.  It's possible the latter expression
  102.   won't work effectively with some makes of joystick or at the speed of some
  103.   chips.                                                                      }
  104.  
  105.     if ScreenX > 78 then ScreenX := 78;        { make sure      }
  106.     if ScreenX < 2 then ScreenX := 2;          { the 'X '       }
  107.     if ScreenY > 23 then ScreenY := 23;        { doesn't get    }
  108.     if ScreenY < 6 then ScreenY := 6;          { out of the box }
  109.     gotoxy(ScreenX,ScreenY);
  110.     write('X');
  111.     delay(30);                         { slow things down a bit }
  112.     if keypressed
  113.       then begin
  114.              read(kbd,ch);
  115.              if upcase(ch) = 'Q' then Done := true;
  116.            end;
  117.   until Done;
  118.   clrscr;
  119. end.
  120.